package com.dog.basics.admin.controller.common;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dog.basics.bean.dto.admin.common.UserAddDto;
import com.dog.basics.bean.dto.admin.common.UserEditDto;
import com.dog.basics.bean.dto.admin.common.UserFromDto;
import com.dog.basics.bean.model.common.DogUser;
import com.dog.basics.bean.vo.admin.common.PcAllUserPageVo;
import com.dog.basics.bean.vo.admin.common.PcUserPageVo;
import com.dog.basics.bean.vo.api.common.UserInfoVo;
import com.dog.basics.common.annotation.SysLog;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.common.util.R;
import com.dog.basics.service.common.IDogUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * APP用户表 前端控制器
 * </p>
 *
 * @author guoy
 * @since 2020-03-28
 */
@RestController
@RequestMapping("/admin/user")
@Api(value = SwaggerConstants.COMMON + "APP用户管理模块", tags = SwaggerConstants.COMMON + "APP用户管理模块")
@AllArgsConstructor
public class PcUserController {
    private final IDogUserService cljgUserService;

    /**
     * @Description: 分页APP用户列表
     * @Param: status
     * @return: IPage<CompanyPageVo>
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "分页APP用户列表", notes = "pc:user:page")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pc:user:page')")
    public R<IPage<PcUserPageVo>> page(PageParam page, @Valid UserFromDto obj) {
        return R.ok(cljgUserService.getPageList(page, obj));
    }

    /**
     * @Description: 全部用户列表
     * @Param: status
     * @return: IPage<CompanyPageVo>
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "全部用户列表", notes = "pc:user:allpage")
    @GetMapping("/allpage")
    // @PreAuthorize("@pms.hasPermission('pc:user:allpage')")
    public R<List<PcAllUserPageVo>> allpage() {
        return R.ok(cljgUserService.getAllPageList());
    }

    /**
     * 添加用户信息
     *
     * @param dto
     * @return guonh 2020年5月18日
     */
    @ApiOperation(value = "添加用户信息", notes = "添加用户信息 (pc:user:add)")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pc:user:add')")
    public R<Boolean> addUser(@RequestBody @Valid UserAddDto dto) {
        return R.ok(cljgUserService.addUser(dto));
    }

    /***
     * 修改用户信息
     * @param dto
     * @return
     * guonh 2020年5月18日
     */
    @ApiOperation(value = "修改用户信息", notes = "修改用户信息 (pc:user:add)")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pc:user:add')")
    public R<Boolean> editUser(@RequestBody @Valid UserEditDto dto) {
        return R.ok(cljgUserService.editUser(dto));
    }


    /**
     * @Description: 查看APP用户
     * @Param: id
     * @return: CompanyInfoVo
     * @Author: wph
     * @Date: 2020/3/27 0027
     */
    @ApiOperation(value = "查看APP用户", notes = "pc:user:info")
    @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true)
    @GetMapping("/info/{id}")
    @PreAuthorize("@pms.hasPermission('pc:user:info')")
    public R<UserInfoVo> info(@PathVariable("id") Long id) {
        DogUser user = cljgUserService.getById(id);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtil.copyProperties(user, userInfoVo);
        return R.ok(userInfoVo);
    }

    /**
     * @Description: 冻结用户
     * @Param: Boolean
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/9  0027
     */
    @SysLog("冻结用户")
    @ApiOperation(value = "冻结用户", notes = "pc:user:nouse")
    @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true)
    @PutMapping("/user/{id}")
    @PreAuthorize("@pms.hasPermission('pc:user:nouse')")
    public R<Boolean> nouse(@PathVariable("id") Long id) {
        return R.ok(cljgUserService.nouse(id));
    }

    /**
     * @Description: 重置密码
     * @Param: Boolean
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/4/9  0027
     */
    @SysLog("重置密码")
    @PutMapping("/resetPassword/{id}")
    @ApiImplicitParam(value = "id", name = "id", dataType = "Long", required = true)
    @ApiOperation(value = "重置密码", notes = "pc:user:resetPassword")
    @PreAuthorize("@pms.hasPermission('pc:user:resetPassword')")
    public R<Boolean> editResetPassword(@PathVariable("id") Long id) {
        return R.ok(cljgUserService.editResetPassword(id));
    }
}
